<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>连接数据库</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.html">教程</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.selectdb.html">获取数据库实例</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.tutorial.connecting" class="section">
  <h2 class="title">连接数据库</h2>
  <p class="para">
   使用下面列出的其中一种方法链接：
  </p>

  <div class="example" id="mongo.tutorial.connecting-example">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;连接到&nbsp;localhost:27017<br /></span><span style="color: #0000BB">$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"mongodb://example.com"&nbsp;</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;连接到远程服务器&nbsp;（使用默认端口：&nbsp;27017）<br /></span><span style="color: #0000BB">$connection&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"mongodb://example.com:65432"&nbsp;</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;链接到远程服务器，使用自定义的端口<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  <p class="para">
   你并不必须手动从服务器断开连接。这个驱动使用了持久连接，并会在下次试图链接到同一服务器时重用它。
  </p>
  <div class="section" id="mongo.tutorial.connecting.seealso">
   <h2 class="title">参见</h2>
   <p class="para">
    <a href="mongo.connecting.html" class="link">链接</a>
    中的相关章节。来获得更多链接方式的信息。
   </p>
   <p class="para">
    <a href="class.mongoclient.html" class="classname">MongoClient</a> 类和
    <span class="function"><a href="mongoclient.construct.html" class="function">MongoClient::__construct()</a></span>
    的文档中有对所有链接方式的详细说明，
    并且带有一些例子。
   </p>
  </div>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.tutorial.html">教程</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.tutorial.selectdb.html">获取数据库实例</a></div>
 <div class="up"><a href="mongo.tutorial.html">教程</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
